package com.xiaoshuidi.cloud.module.member.repository;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.xiaoshuidi.cloud.module.member.controller.app.topics.vo.AddLikeVo;
import com.xiaoshuidi.cloud.module.member.mapper.topics.MemberTopicsThumbsRecordMapper;
import com.xiaoshuidi.cloud.module.member.pojo.topics.MemberTopicsThumbsRecord;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor
@Slf4j
public class MemberTopicsThumbsRecordRepository extends ServiceImpl<MemberTopicsThumbsRecordMapper, MemberTopicsThumbsRecord> {
    private final MemberTopicsThumbsRecordMapper mapper;

    public Boolean alreadyAddLike(AddLikeVo vo) {
        LambdaQueryWrapperX<MemberTopicsThumbsRecord> wrapperX = new LambdaQueryWrapperX<MemberTopicsThumbsRecord>()
                .eqIfPresent(MemberTopicsThumbsRecord::getTopicPostId, vo.getTopicPostId())
                .eqIfPresent(MemberTopicsThumbsRecord::getUserId, vo.getUserId());
        List<MemberTopicsThumbsRecord> list = mapper.selectList(wrapperX);
        if (list != null && list.size() > 0) {
            return false;
        }
        return true;

    }

    public void updateUserId(Long oldUserId, Long userId) {
        //查看旧账号的所有点赞记录
        LambdaQueryWrapperX<MemberTopicsThumbsRecord> wrapperX = new LambdaQueryWrapperX<MemberTopicsThumbsRecord>()
                .eqIfPresent(MemberTopicsThumbsRecord::getUserId, oldUserId);
        List<MemberTopicsThumbsRecord> list = mapper.selectList(wrapperX);
        //
        if (list != null && list.size() > 0) {
            List<Long> topicPostIds = list.stream().map(MemberTopicsThumbsRecord::getTopicPostId).collect(Collectors.toList());
            LambdaQueryWrapperX<MemberTopicsThumbsRecord> wrapper = new LambdaQueryWrapperX<MemberTopicsThumbsRecord>()
                    .eqIfPresent(MemberTopicsThumbsRecord::getUserId, userId)
                    .in(MemberTopicsThumbsRecord::getTopicPostId, topicPostIds);
            List<MemberTopicsThumbsRecord> list2 = mapper.selectList(wrapper);
            //找到list中list2中沒有的
            if (list2!=null&&list2.size()>0) {
                List<Long> collect = list2.stream().map(MemberTopicsThumbsRecord::getTopicPostId).collect(Collectors.toList());
                List<Long> containsCollect = topicPostIds.stream().filter(o -> collect.contains(o)).collect(Collectors.toList());
                List<Long> notContainsCollect = topicPostIds.stream().filter(o -> !collect.contains(o)).collect(Collectors.toList());

                //list2里包含的全部删除
                mapper.deleteByTopicPostId(containsCollect, oldUserId);
                //list2里不包含的全部替换
                mapper.updateUserId(oldUserId, userId, notContainsCollect);
            }else{
                //list2里不包含的全部替换
                mapper.updateUserId(oldUserId, userId, topicPostIds);
            }
        }
    }
}
